﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Reflection;
using System.Data;
using System.IO;

namespace Brilliant.ORM
{
    /// <summary>
    /// DBProvider一个静态实例
    /// </summary>
    public static class DBHelper
    {
        private static readonly DBProvider dbProvider = new DBProvider();

        /// <summary>
        /// 获取连接字符串
        /// </summary>
        public static string ConnectionString
        {
            get { return dbProvider.ConnectionString; }
        }

        /// <summary>
        /// 获取ProviderName
        /// </summary>
        public static string ProviderName
        {
            get { return dbProvider.ProviderName; }
        }

        /// <summary>
        /// 数据访问对象实例
        /// </summary>
        public static IDataProvider DataProvider
        {
            get { return dbProvider.DataProvider; }
        }

        /// <summary>
        /// 数据库分页方式
        /// </summary>
        public static PagedType DataPagedType
        {
            get { return dbProvider.DataPagedType; }
            set { dbProvider.DataPagedType = value; }
        }

        /// <summary>
        /// 返回一个新的DataParameter对象实例
        /// </summary>
        /// <returns>DataParameter对象</returns>
        public static IDbDataParameter GetParameter()
        {
            return dbProvider.GetParameter();
        }

        /// <summary>
        /// 开启事务
        /// </summary>
        public static void BeginTransaction()
        {
            dbProvider.BeginTransaction();
        }

        /// <summary>
        /// 提交事务
        /// </summary>
        public static void Commit()
        {
            dbProvider.Commit();
        }

        /// <summary>
        /// 回滚事务
        /// </summary>
        public static void Rollback()
        {
            dbProvider.Rollback();
        }

        /// <summary>
        /// 更改连接属性
        /// </summary>
        /// <param name="name">属性名称</param>
        public static void ChangeConnectionString(string name)
        {
            dbProvider.ChangeConnectionString(name);
        }
    }

    /// <summary>
    /// 数据库帮助类
    /// </summary>
    public class DBProvider
    {
        #region 字段
        private string connectionString;
        private string providerName;
        private IDataProvider dataProvider;
        private PagedType dataPagedType;
        #endregion

        #region 属性
        /// <summary>
        /// 获取连接字符串
        /// </summary>
        public string ConnectionString
        {
            get { return connectionString; }
        }

        /// <summary>
        /// 获取ProviderName
        /// </summary>
        public string ProviderName
        {
            get { return providerName; }
        }

        /// <summary>
        /// 数据访问对象实例
        /// </summary>
        public IDataProvider DataProvider
        {
            get { return dataProvider; }
        }

        /// <summary>
        /// 数据库分页方式
        /// </summary>
        public PagedType DataPagedType
        {
            get { return dataPagedType; }
            set { dataPagedType = value; }
        }
        #endregion

        #region 构造器
        /// <summary>
        /// 构造器
        /// </summary>
        public DBProvider()
            : this("default")
        {

        }

        /// <summary>
        /// 构造器
        /// </summary>
        /// <param name="connProperty">连接字符串属性节点</param>
        public DBProvider(string connProperty)
        {
            ConnectionStringSettingsCollection csc = ConfigurationManager.ConnectionStrings;
            connectionString = csc[connProperty].ConnectionString;
            providerName = csc[connProperty].ProviderName;
            CreateProvider();
        }
        #endregion

        #region 方法
        /// <summary>
        /// 返回一个新的DataParameter对象实例
        /// </summary>
        /// <returns>DataParameter对象</returns>
        public IDbDataParameter GetParameter()
        {
            return this.dataProvider.GetParameter();
        }

        /// <summary>
        /// 开启事务
        /// </summary>
        public void BeginTransaction()
        {
            this.dataProvider.BeginTransaction();
        }

        /// <summary>
        /// 提交事务
        /// </summary>
        public void Commit()
        {
            this.dataProvider.Commit();
        }

        /// <summary>
        /// 回滚事务
        /// </summary>
        public void Rollback()
        {
            this.dataProvider.Rollback();
        }

        /// <summary>
        /// 更改连接属性
        /// </summary>
        /// <param name="name">属性名称</param>
        public void ChangeConnectionString(string name)
        {
            this.connectionString = ConfigurationManager.ConnectionStrings[name].ConnectionString;
            this.providerName = ConfigurationManager.ConnectionStrings[name].ProviderName;
            this.CreateProvider();
        }

        /// <summary>
        /// 创建Provider对象
        /// </summary>
        private void CreateProvider()
        {
            Type type = null;
            if (!String.IsNullOrEmpty(providerName))
            {
                string namePrefix = "Brilliant.ORM.";
                string className = providerName;
                string assemblyPath = String.Empty;
                if (providerName.Contains(namePrefix))
                {
                    className = providerName.Replace(namePrefix, "");
                }
                else
                {
                    providerName = namePrefix + providerName;
                }
                if (providerName != typeof(SqlServer).FullName && providerName != typeof(Odbc).FullName && providerName != typeof(OleDb).FullName)
                {
                    assemblyPath = String.Format("Brilliant.DataProvider.{0}.dll", className);
                    if (!File.Exists(assemblyPath))
                    {
                        assemblyPath = String.Format(@"{0}\bin\Brilliant.DataProvider.{1}.dll", AppDomain.CurrentDomain.BaseDirectory, className);
                    }
                    Assembly assembly = Assembly.LoadFrom(assemblyPath);
                    type = assembly.GetType(providerName);
                }
                else
                {
                    type = Type.GetType(providerName, true);
                }
            }
            else
            {
                type = typeof(SqlServer);
            }
            object obj = Activator.CreateInstance(type, connectionString);
            dataProvider = (IDataProvider)obj;
            dataPagedType = dataProvider.DataPagedType;
        }
        #endregion
    }

    /// <summary>
    /// 分页类型枚举
    /// </summary>
    public enum PagedType
    {
        /// <summary>
        /// 通用数据库分页
        /// </summary>
        Common,

        /// <summary>
        /// 用于Oracle数据库分页
        /// </summary>
        RowId,

        /// <summary>
        /// 用于SQL Server2005以上数据库分页
        /// </summary>
        RowNumber,

        /// <summary>
        /// 用于MySql以及SQLite数据库分页
        /// </summary>
        Limit
    }
}
